package com.junzhiit.nengji.shop.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.shop.service.AddressService;
import com.junzhiit.nengji.shop.web.vo.AddressAddVO;
import com.junzhiit.nengji.shop.web.vo.AddressEditVO;
import com.junzhiit.nengji.shop.web.vo.ResultVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/address")
public class AddressController extends BaseController {

	@Autowired
	private AddressService addressService;

	@ApiOperation(value = "获取用户所有的收货地址列表")
	@RequestMapping(method = RequestMethod.GET)
	public ResultVO findAll(String userId) {
		AssertUtil.notBlank(userId, "userId is required.");
		return addressService.findAll(userId);
	}

	@ApiOperation(value = "根据收货地址ID查询收货地址")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ResultVO findById(@PathVariable String id) {
		AssertUtil.notBlank(id, "id is required.");
		return addressService.findById(id);
	}

	@ApiOperation(value = "创建收货地址")
	@RequestMapping(method = RequestMethod.POST)
	public ResultVO add(AddressAddVO addressAddVO) {
		AssertUtil.notBlank(addressAddVO.getUserId(), "userId is required.");
		AssertUtil.notBlank(addressAddVO.getName(), "name is required.");
		AssertUtil.notBlank(addressAddVO.getPhone(), "phone is required.");
		AssertUtil.notBlank(addressAddVO.getArea(), "area is required.");
		AssertUtil.notBlank(addressAddVO.getAddress(), "address is required.");
		return addressService.add(addressAddVO);
	}

	@ApiOperation(value = "修改收货地址")
	@RequestMapping(method = RequestMethod.PUT)
	public ResultVO update(AddressEditVO addressEditVO) {
		AssertUtil.notBlank(addressEditVO.getId(), "id is required.");
		return addressService.edit(addressEditVO);
	}
	
	@ApiOperation(value = "删除收货地址")
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public ResultVO delAddress(@PathVariable String id,String userId) {
		AssertUtil.notBlank(id, "id is required.");
		return addressService.delete(id,userId);
	}
	
	@ApiOperation(value = "更新地址状态（更新默认地址）")
	@RequestMapping(value = "/default/{id}",method = RequestMethod.PUT) 
	public ResultVO updateStatus(String id,String userId){
		AssertUtil.notBlank(id, "id is required.");
		AssertUtil.notBlank(userId, "userId is required.");
		return addressService.updateStatus(id,userId);
	}
	
	@ApiOperation(value = "勿用，更新")
	@RequestMapping(value = "/update",method=RequestMethod.PUT)
	public void update(){
		addressService.update();
	}
}
